<?php
namespace app\common\logic;

use app\common\library\exception\ApiException;
use app\common\library\MemberVo;
use app\common\model\Car;
use app\common\model\CarImages;
use app\common\model\CarImagesShop;
use app\common\model\CarImagesShopRecord;
use app\common\model\Member;
use app\common\model\Order;
use think\Env;


class CustomerLogic extends BaseLogic
{
    public $model;

    public function assignModel()
    {
        $this->model          = new Member();
    }

    /**
     * 列表
     * @param array $params
     */
    public function index($shopId,$keyword)
    {
        return Order::shopMember($shopId,$keyword);
    }

    /**
     * 信息
     * @param array $params
     */
    public function info($shopId,$memberId)
    {
        $member_info = Member::get($memberId);
        if (!$member_info)
            throw new ApiException('客户信息不存在',404);
        $car_info = Car::get(['member_id'=>$memberId,'is_default'=>1],['brand','series']);
        $money = Order::where(['shop_id'=>$shopId,'member_id'=>$memberId,'order_status'=>['in',[Order::ORDER_STATUS_WAIT_COMMENT, Order::ORDER_STATUS_OK]]])->sum('order_amount');//累计消费金额
        $last_time = Order::where(['shop_id'=>$shopId,'member_id'=>$memberId,'order_status'=>['in',[Order::ORDER_STATUS_WAIT_COMMENT, Order::ORDER_STATUS_OK]]])->order('audit_time desc')->value('audit_time'); //获取最近到店时间（核销时间）
        $data['member_info'] = [
            'member_id'=>$member_info['id'],
            'member_avatar'=>$member_info['member_avatar'],
            'member_mobile'=>$member_info['member_mobile'],
            'member_name'=>$member_info['member_name'],
            'balance'=>$member_info['balance'],
            'default_shop_id'=>$member_info['default_shop_id'],
            'is_vip'=>$member_info['is_vip'],
            'consume'=>$money,
            'last_time'=>getFriendTime($last_time)
        ];
        $data['car_info'] = ['id'=>$car_info['id'],'car_numbers'=>$car_info['car_numbers'],'brand'=>$car_info['brand']['name'].'-'.$car_info['series']['name'],'vin'=>$car_info['vin'],'car_type'=>$car_info['car_type']];
        $data['car_num'] = Car::where(['member_id'=>$memberId,'is_del'=>0])->count('id');
        return $data;
    }

    /**
     * 车辆信息切换
     * @param array $params
     */
    public function car_exchange($params)
    {
        $current_car = Car::where(['is_del'=>0,'id'=>$params['car_id']])->find();
        if (!$current_car){
            throw new ApiException('查无该车辆信息',404);
        }
        $car_info = Car::where(['car.member_id'=>$current_car['member_id'],'car.is_del'=>0,'car.id'=>['gt',$params['car_id']]])->with(['brand','series'])->order('car.id asc')->find();
        if (empty($car_info)){
            $car_info = Car::where(['car.member_id'=>$current_car['member_id'],'car.is_del'=>0,'car.id'=>['lt',$params['car_id']]])->with(['brand','series'])->order('car.id asc')->find();
            if (empty($car_info))
                return [];
        }
        $data['car_info'] = ['id'=>$car_info['id'],'car_numbers'=>$car_info['car_numbers'],'brand'=>$car_info['brand']['name'].'-'.$car_info['series']['name'],'vin'=>$car_info['vin'],'car_type'=>$car_info['car_type']];
        return $data;
    }


    /**
     * 信息
     * @param array $params
     */
    public function orders($params)
    {
        $pageList       = Order::shopMemberOrders(MemberVo::instance()->getShopId(),$params);
        return $pageList;
    }

    /**
     * 更多信息
     * @param $car_id 车辆ID
     * @param $type 类型 0车主端  1商家端
     * @return array
     * @throws \think\exception\DbException
     */
    public function moreInfo($params)
    {
        if ($params['type'] == 0){
            $imgs = CarImages::all(['car_id'=>$params['car_id']])->toArray();
        }else{
            $imgs = CarImagesShop::all(['car_id'=>$params['car_id']])->toArray();

        }
        if (!empty($imgs)){
            foreach ($imgs as &$v){
                $v['type'] = 'img_'.$v['type'];
            }
        }
        $imgs_new = array_column($imgs,NULL,'type');
        $data[] = ['name' => '车辆详情图-证件照', 'list' => [
            ['name' => '行驶证正面照', 'type' => 1, 'url' => isset($imgs_new['img_1']) ? $imgs_new['img_1']['url'] : ''],
            ['name' => '行驶证反面照', 'type' => 2, 'url' => isset($imgs_new['img_2']) ? $imgs_new['img_2']['url'] : '']]];

        $data[] = ['name' => '车辆详情图-车外观', 'list' => [
            ['name' => '车外观左侧照', 'type' => 3, 'url' => isset($imgs_new['img_3']) ? $imgs_new['img_3']['url'] : ''],
            ['name' => '车外观右侧照', 'type' => 4, 'url' => isset($imgs_new['img_4']) ? $imgs_new['img_4']['url'] : ''],
            ['name' => '车外观车头照', 'type' => 5, 'url' => isset($imgs_new['img_5']) ? $imgs_new['img_5']['url'] : ''],
            ['name' => '车外观车尾照', 'type' => 6, 'url' => isset($imgs_new['img_6']) ? $imgs_new['img_6']['url'] : '']]];

        $data[] = ['name' => '车辆详情图-车内饰', 'list' => [
            ['name' => '主驾驶照', 'type' => 7, 'url' => isset($imgs_new['img_7']) ? $imgs_new['img_7']['url'] : ''],
            ['name' => '副驾驶照', 'type' => 8, 'url' => isset($imgs_new['img_8']) ? $imgs_new['img_8']['url'] : ''],
            ['name' => '中控台照', 'type' => 9, 'url' => isset($imgs_new['img_9']) ? $imgs_new['img_9']['url'] : ''],
            ['name' => '后排照', 'type' => 10, 'url' => isset($imgs_new['img_10']) ? $imgs_new['img_10']['url'] : '']]];

        $data[] = ['name' => '车辆详情图-车轮', 'list' => [
            ['name' => '左前轮照', 'type' => 11, 'url' => isset($imgs_new['img_11']) ? $imgs_new['img_11']['url'] : ''],
            ['name' => '左后轮照', 'type' => 12, 'url' => isset($imgs_new['img_12']) ? $imgs_new['img_12']['url'] : ''],
            ['name' => '右前轮照', 'type' => 13, 'url' => isset($imgs_new['img_13']) ? $imgs_new['img_13']['url'] : ''],
            ['name' => '右后轮照', 'type' => 14, 'url' => isset($imgs_new['img_14']) ? $imgs_new['img_14']['url'] : '']]];

        $data[] = ['name' => '车辆详情图-发动机/蓄电池', 'list' => [
            ['name' => '发动机舱照', 'type' => 15, 'url' => isset($imgs_new['img_15']) ? $imgs_new['img_15']['url'] : ''],
            ['name' => '蓄电池照', 'type' => 16, 'url' => isset($imgs_new['img_16']) ? $imgs_new['img_16']['url'] : ''],
        ]];
        if ($params['type'] == 1){
            foreach ($data as &$item){
                foreach ($item['list'] as &$v){
                    $info = CarImagesShopRecord::get(['type'=>$v['type'],'car_id'=>$params['car_id'],'shop_id'=>MemberVo::instance()->getShopId()]);//获取店铺上传的未审核的图片
                    $v['status'] = '';
                    if ($info){
                        if ($info['status'] != 1){
                            $v['url'] = $info['url'];
                            $v['status'] = $info['status'];
                        }else{
                            if (!empty($v['url']))
                                $v['status'] = 1;
                        }
                    }else{
                        if (!empty($v['url']))
                            $v['status'] =1;
                    }
                }
            }
        }
        return $data;
    }



}